require('../common/header/index')
var _store_service = require('../../service/store-service')
var _product_service = require('../../service/product-service')
var _order_service = require('../../service/order-service')
const vue = require('../common/vue');
const _util = require('../../util/util');
const echarts = require('../common/echarts')
let _mystore_content = new vue({
    el: '#mainBody',
    data() {
        return {
            store: {
                shopid: 0,
                name: '',
                address: '',
                salesvolume: 5,
                favorablerate: 5.0,
                sellername: '',
                avaiable: 1
                // description: '',
            },
            // mystores: [], 先填一些数据
            mystores: [{
                "shopid": 1,
                "name": "自律母婴店",
                "address": "湖南娄底",
                "salesvolume": 666,
                "favorablerate": 4.6,
                "sellername": "黄舒杰",
                "available": 1
            }, {
                "shopid": 3,
                "name": "乐尔乐超市",
                "address": "湖南娄底",
                "salesvolume": 666,
                "favorablerate": 4.6,
                "sellername": "黄舒杰",
                "available": 1
            },],
            orderid: '',
            refusalReason: '',
            refundReason: '',
            isconfirm: 1,
            orderList: [],
            orderInfo: '',
        }
    },
    methods: {
        addNewStore: function (event) {
            _store_service.addNewStore(
                this.store,
                function (res) {
                    alert('添加成功！')
                    window.location.href = '/view/mystore.html'
                },
                function (errMsg) {
                    console.log(errMsg)
                }
            )
        },
        sendToDetail: function (shopid, event) {
            window.location.href = '/view/mystore_detail.html?shopid=' + shopid;
        },
        reset: function () {
            this.store.shopid = '';
            this.store.name = '';
            this.store.address = '';
            this.store.sellername = '';
            this.store.description = '';
        },
        getShopItems: function (shopid, name, index) {
            var self = this
            _product_service.getItemsByShopId(
                shopid,
                0,
                function (res) {
                    let divId = 'charts' + index;
                    let shopname = name;
                    let categoryList = [];
                    for (let j = 0; j < res.length; j++) {
                        categoryList[j] = res[j].id;
                    }
                    let starList = [];
                    for (let j = 0; j < res.length; j++) {
                        starList[j] = res[j].star;
                    }
                    self.drawABar(divId, shopname, categoryList, starList)
                },
                function (errMsg) {
                    console.log(errMsg);
                }
            )
        },
        drawAPie: function (divId) {
            var charts = echarts.init(document.getElementById(divId))
            let dataList = [];
            for (let i = 0; i < this.mystores.length; i++) {
                dataList[i] = { value: this.mystores[i].salesvolume, name: this.mystores[i].name }
            }
            charts.setOption({
                title: {
                    text: '销售量分布'
                },
                tooltip: {
                    trigger: 'item',
                },
                legend: {
                    top: '5%',
                    left: 'center',
                },
                series: [
                    {
                        name: '销售量(件)',
                        type: 'pie',
                        redius: ['40%', '70%'],
                        avoidLabelOverlap: false,
                        itemStyle: {
                            borderRadius: 10,
                            borderColor: '#fff',
                            borderWidth: 2,
                        },
                        label: {
                            show: false,
                            position: 'center'
                        },
                        emphasis: {
                            label: {
                                show: true,
                                fontSize: '40',
                                fontWeight: 'bold',
                            }
                        },
                        labelLine: {
                            show: false
                        },
                        data: dataList
                    }
                ]
            })
        },
        drawABar: function (divId, shopname, categoryList, starList) {
            var charts = echarts.init(document.getElementById(divId))
            charts.setOption({
                title: {
                    text: shopname
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                xAxis: {
                    type: 'category',
                    data: categoryList
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: '好评率',
                        type: 'bar',
                        data: starList,
                        showBackground: true,
                        backgroundStyle: {
                            color: 'rgba(180,180,180,0.2)'
                        }
                    }
                ]
            })
        },
        deliver: function (orderid) {
            _order_service.deliver(
                orderid,
                function (res) {
                    alert('确认发货！')
                    window.location.href = '/view/mystore.html'
                },
                function (errMsg) {
                    console.log(errMsg)
                }
            )
        },
        updateReturn: function (isConfirm) {
            let self = this;
            if (isConfirm == 1) {
                _order_service.confirmReturn(
                    self.orderid,
                    function (res) {
                        alert('确认退货')
                        window.location.href = '/view/mystore.html'
                    },
                    function (errMsg) {
                        console.log(errMsg)
                    }
                )
            } else {
                _order_service.refuseReturn(
                    self.orderid,
                    self.refusalReason,
                    function (res) {
                        alert('拒绝收货！')
                        window.location.href = '/view/mystore.html'
                    },
                    function (errMsg) {
                        console.log(errMsg)
                    }
                )
            }
        },
        recordId: function (orderid) {
            let self = this;
            this.orderid = orderid;
            _order_service.getRefundInfo(
                orderid,
                function (res) {
                    self.refundReason = res.refundreason;
                },
                function (errMsg) {
                    console.log(errMsg)
                }
            )
        },
        getRefundInfo: function () {
            let self = this;
            _order_service.getRefundInfo(
                self.orderid,
                function (res) {
                    self.refundReason = res.refundreason
                },
                function (errMsg) {
                    console.log(errMsg)
                }
            )
        },
        showDetail: function (orderid) {
            window.location.href = '/view/mystore_order_detail.html?orderID=' + orderid;
        }
    }
})
var _mystore = {
    _this: this,
    init: function () {
        this.bindEvent();
        this.getAllStores();
        this.getAllOrders();
    },
    bindEvent: function () {

    },
    getAllStores: function () {
        _store_service.getAllStores(
            function (res) {
                _mystore_content.mystores = res
                _mystore_content.drawAPie('charts00');
                for (let i = 0; i < _mystore_content.mystores.length; i++) {
                    _mystore_content.getShopItems(_mystore_content.mystores[i].shopid, _mystore_content.mystores[i].name, i);
                }
            },
            function (errMsg) {
                console.log(errMsg)
            }
        )
    },
    getAllOrders: function () {
        _order_service.getAllOrders(
            -1,
            function (res) {
                _mystore_content.orderList = res;
            },
            function (errMsg) {
                console.log(errMsg)
            }
        )
    }
}
module.exports = _mystore.init()
